package com.oracle.calllog.etl.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/******************************************
 * @ClassName HBaseConnectionUtil
 * @Description 与HBase集群建立连接的工具类
 * @Author Mr.Chen
 * @Date 2022/6/11 21:13
 * @Version 1.0
 *****************************************/
public class HBaseConnectionUtil {
    private static Connection conn;
//    private static Configuration hbaseConf;

    /**
     * 获取连接的方法：如果不存在连接，需要创建与HBase的连接，ZooKeeper服务器信息在hbase-site.xml文件中
     * @param conf
     * @return
     */
    public static synchronized Connection getConnection(Configuration conf) {
//        if (hbaseConf == null){
//            hbaseConf = HBaseConfiguration.create(conf);
//        }
        try {
            if (conn == null || conn.isClosed()) {
                conn = ConnectionFactory.createConnection(conf);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭连接的方法
     */
    public static synchronized void closeConnection() {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
